import logging
from typing import Dict, Any, Optional
from abc import ABC, abstractmethod

class BaseAgent(ABC):
    """智能体基类"""
    
    def __init__(self):
        self.logger = logging.getLogger(self.__class__.__name__)
        
    @abstractmethod
    async def process(self, input_data: Dict) -> Optional[Dict]:
        """处理输入数据"""
        pass
        
    def _validate_input(self, input_data: Dict) -> bool:
        """验证输入数据"""
        return True

    def _format_output(self, output_data: Dict) -> Dict:
        """格式化输出"""
        return output_data

    async def execute(self, input_data: Dict) -> Optional[Dict]:
        """执行智能体任务"""
        try:
            if not self._validate_input(input_data):
                raise ValueError("Invalid input data")
                
            result = await self.process(input_data)
            if result:
                return self._format_output(result)
                
        except Exception as e:
            self.logger.error(f"执行失败: {str(e)}")
        return None
